<?php
    
class url 
{
    /**
	 * 获取当前运行程序的网址域名
	 * 
	 * 如：http://www.fvzone.com
	 * @access public
	 * @return string	网址(域名)
	 */
	public function host()
    {
		//获取网址域名部分
		$host = !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];

        //获取端口
        $port = ($_SERVER['SERVER_PORT'] != '80' && strrpos($host, $_SERVER['SERVER_PORT']) === false) ? ':'.$_SERVER['SERVER_PORT'] : '';

		//获取网络协议
		$secure = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';
		return $secure.$host.$port;
	}
	
	/**
	 * 获取当前项目的根目录的URL
	 * 
	 * 用于网页的CSS, JavaScript，图片等文件的调用.
	 * @access public
	 * @return string 	根目录的URL. 注:URL以反斜杠("/")结尾
	 */
	public function base($host = false) 
    {
        $subfolder = dirname($_SERVER['SCRIPT_NAME']).'/';

		return ($host ? self::host() : '').$subfolder;
	}
	
	/**
	 * 获取当前Controller内的某Action的URL
	 * 
	 * 获取当前控制器(Controller)内的动作(Action)的URL. 注:该网址仅由项目入口文件和控制器(Controller)组成。
	 * @access public
	 * @param string $action_name 所要获取URL的action的名称
	 * @return string	URL
	 */
	public function action($name = null)
    {
        $name = $name ? $name : Request::action();
		return self::build(Request::controller() .URL_SPLIT. $name);
	}
	
	/**
	 * 获取当前项目asset目录的URL
	 * 
	 * @access public
	 * @param string $dir_name 子目录名
	 * @return string	URL
	 */
	public function asset($dir_name = null)
    {
		//获取assets根目录的url
		$asset_url = self::base(true) . 'assets/';
		
		//分析assets目录下的子目录
		if (!is_null($dir_name))
			$asset_url .= $dir_name . '/';
		
		return $asset_url;
	}

    /**
     * 获取当前项目themes目录的URL
     *
     * @access public
     * @param string $theme_name 所要获取URL的主题名称
     * @return string    URL
     */
    public function theme($name = null)
    {
        //分析主题名
        $name = is_null($name) ? 'default' : $name;

        return self::base(true) . 'themes/' . $name . '/';
    }
	
	/**
	 * 网址(URL)组装操作
	 * 
	 * 组装绝对路径的URL
	 * @access public
	 * @param string 	$route 			controller与action
	 * @param array 	$params 		URL路由其它字段
	 * @param boolean 	$routing_mode	网址是否启用路由模式
	 * @return string	URL
	 */
	public function build($route, $params = null)
    {
        //组件不是默认组件
        if (Request::component() != PUBLIC_COMPONENT)
            $route = Request::component().URL_SPLIT.$route;

		$route 	= str_replace('/', URL_SPLIT, $route);
		$url  	= self::base() . ((REWRITE == false) ? SCRIPT_NAME . URL_SPLIT : '') . $route;
		
		//参数$params变量的键(key),值(value)的URL组装
		if (!is_null($params) && is_array($params))
        {
			$params_url = array();
			if (REWRITE == false)
            {				
				foreach ($params as $key => $value)
                {
					$params_url[] = trim($key) . '=' . trim($value);
				}
				$url .= URL_SPLIT . '?' . implode('&', $params_url);
			} 
            else 
            {
				foreach ($params as $key=>$value)
                {
					$params_url[] = trim($key) . URL_SPLIT . trim($value);
				}
				$url .= URL_SPLIT . implode(URL_SPLIT, $params_url) . URL_SUFFIX;
			}
		}
		return str_replace('//', URL_SPLIT, $url);
	}
}